﻿#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <mysql/mysql.h>
#include "cJSON.h"

#define REMOTE_IP  "192.168.0.186"
#define REMOTE_PORT 9876
#define leavefield 5
//请假结构体
typedef struct leaveinfo
{
	char type[20];
	char name[20];
	char leavetime[20]; 
	char backtime[20];
	char leavecause[100];
	char approval_status[20];
}LEAVE;
/*
函数功能：打包json包
函数参数：LEAVE *leave-请假结构体
函数返回值：char *
*/
char * LeavePack(LEAVE *leave);
/*
函数功能：请假提交
函数参数：LEAVE leave-请假结构体，sc-服务器套接字
函数返回值：void
*/
void LeaveSubmit(LEAVE leave,int sc);
/*
函数功能：显示自己的请假信息
函数参数：LEAVE leave-请假结构体，sc-服务器套接字
函数返回值：int
*/
int CheckLeavemyself(LEAVE leave,int sc);
/*
函数功能：修改自己未审批的请假信息
函数参数：LEAVE leave-请假结构体，sc-服务器套接字
函数返回值：int
*/
int UpdateLeavemyself(LEAVE leave,int sc);
/*
函数功能：审批员工的请假信息
函数参数：LEAVE leave-请假结构体，sc-服务器套接字
函数返回值：int
*/
int ApprovalLeave(LEAVE leave,int sc);
/*
函数功能：按照姓名查找员工的请假信息
函数参数：LEAVE leave-请假结构体，sc-服务器套接字
函数返回值：int
*/
int CheakLeaveName(LEAVE leave,int sc);
/*
函数功能：按照时间查找员工的请假信息
函数参数：LEAVE leave-请假结构体，sc-服务器套接字
函数返回值：int
*/
int CheakLeaveTime(LEAVE leave,int sc);

char p[1024] = {0};
int main(int argc,const char* argv[])
{
	int sc = socket(AF_INET,SOCK_STREAM,0);

	if(sc == -1)
	{
		perror("socket");
		return -1;
	}

	char buf[1024] = {0};
	struct sockaddr_in raddr;
	raddr.sin_family = AF_INET;
	raddr.sin_port = htons(REMOTE_PORT);
	raddr.sin_addr.s_addr = inet_addr(REMOTE_IP);
	int iscon = connect(sc,(struct sockaddr*)&raddr,sizeof(raddr));
	if (iscon == -1)
	{
		perror("connect");
		exit(1);
	}
	LEAVE leave;
	int c = 0;
	while(1)
	{
		printf("1--请假\n");
		printf("2--显示自己的请假信息\n");
		printf("3--修改自己未审批的请假信息\n");
		printf("4--审批员工的请假信息\n");
		printf("5--按照姓名查找员工的请假信息\n");
		printf("6--按照时间查找员工的请假信息\n");
		printf("-1--退出\n");
		printf("请输入选择\n");
		scanf("%d",&c);
		if (c == -1)
		{
			break;
		}
		switch(c)
		{
			case 1:
			//请假(员工增)
				LeaveSubmit(leave,sc);
				break;
			case 2:
			//显示自己的请假信息(员工删和查)
				CheckLeavemyself(leave,sc);
				break;
			case 3:
			//修改自己未审批的请假信息
				UpdateLeavemyself(leave,sc);
				break;
			case 4:
				//审批员工的请假信息(boss改)
				ApprovalLeave(leave,sc);
				break;
			case 5:
				//按照姓名查找员工的请假信息(boss查)
				CheakLeaveName(leave,sc);
				break;
			case 6:
				//按照时间查找员工的请假信息(boss查)
				CheakLeaveTime(leave,sc);
				break;
		}
	}
	close(sc);
    return 0;
}
//请假打包
char * LeavePack(LEAVE *leave)
{
	//创建一个json对象
	cJSON* root = cJSON_CreateObject();
	//创建一个各数据类型的json对象
	cJSON* type = cJSON_CreateString(leave->type);
	cJSON* name = cJSON_CreateString(leave->name);
	cJSON* leavetime = cJSON_CreateString(leave->leavetime);
	cJSON* backtime = cJSON_CreateString(leave->backtime);
	cJSON* leavecause = cJSON_CreateString(leave->leavecause);
	cJSON* approval_status = cJSON_CreateString(leave->approval_status);
	//向json对象中添加“键-值”对
	cJSON_AddItemToObject(root,"type",type);
	cJSON_AddItemToObject(root,"name",name);
	cJSON_AddItemToObject(root,"leavetime",leavetime);
	cJSON_AddItemToObject(root,"backtime",backtime);
	cJSON_AddItemToObject(root,"leavecause",leavecause);
	cJSON_AddItemToObject(root,"approval_status",approval_status);
	//将打包好的json对象copy到p字符串中
	strcpy(p,cJSON_Print(root));

	//释放json对象
	cJSON_Delete(root);
}
//请假函数
void LeaveSubmit(LEAVE leave,int sc)
{
	memset(&leave,0,sizeof(LEAVE));
	strcpy(leave.type,"leavesubmit");

	printf("请输入请假人员姓名:");
	scanf("%s",leave.name);

	printf("请输入请假时间:");
	scanf("%s",leave.leavetime);

	printf("请输入返回时间:");
	scanf("%s",leave.backtime);

	printf("请输入请假原因:");
	scanf("%s",leave.leavecause);

	strcpy(leave.approval_status,"wait");//wait-待审批yes-同意no-否决
	LeavePack(&leave);
	//json数据包写入sock；
	write(sc,p,strlen(p));
}
//查询自己的请假信息
int CheckLeavemyself(LEAVE leave,int sc)
{
	memset(&leave,0,sizeof(LEAVE));
	strcpy(leave.type,"checkleavemyself");
	printf("input name:");
	scanf("%s",leave.name);
	LeavePack(&leave);//打包
	write(sc,p,strlen(p));
	while(1)
	{
		//清空p中之前的套接字数据
		memset(p,0,sizeof(p));
		int re = read(sc,p,1024);
		if (strncmp(p,"quit",4)==0)
		{
			break;
		}
		if (strncmp(p,"nothing",7)==0)
		{
			printf("查无信息\n");
			break;
		}
		printf("%s",p);
		//给服务器发送接受成功信息isok
		write(sc,"isok",4);
	}
}
//修改自己未审批的请假信息
int UpdateLeavemyself(LEAVE leave,int sc)
{
	int c = 0;
	memset(&leave,0,sizeof(LEAVE));
	strcpy(leave.type,"updateleavemyself");
	printf("input name:");
	scanf("%s",leave.name);
	LeavePack(&leave);//打包
	write(sc,p,strlen(p));
	while(1)
	{
		//清空p中之前的套接字数据
		memset(p,0,sizeof(p));
		int re = read(sc,p,1024);
		if (strncmp(p,"quit",4)==0)
		{
			printf("全部修改完成\n");
			break;
		}
		if (strncmp(p,"nothing",7)==0)
		{
			printf("查无信息\n");
			break;
		}
		printf("%s",p);
		printf("1--修改请假时间\n");
		printf("2--修改返回时间\n");
		printf("3--修改请假原因\n");
		printf("4--下一条\n");
		printf("-1--退出\n");
		scanf("%d",&c);
		if (c == -1)
		{
			return 0;
		}
		switch(c)
		{
			case 1:
				memset(&leave,0,sizeof(LEAVE));
				strcpy(leave.type,"updateleavetime");
				printf("input leavetime:");
				scanf("%s",leave.leavetime);
				LeavePack(&leave);//打包
				write(sc,p,strlen(p));
				break;
			case 2:
				memset(&leave,0,sizeof(LEAVE));
				strcpy(leave.type,"updatebacktime");
				printf("input backtime:");
				scanf("%s",leave.backtime);
				LeavePack(&leave);//打包
				write(sc,p,strlen(p));
				break;
			case 3:
				memset(&leave,0,sizeof(LEAVE));
				strcpy(leave.type,"updateleavecause");
				printf("input leavecause:");
				scanf("%s",leave.leavecause);
				LeavePack(&leave);//打包
				write(sc,p,strlen(p));
				break;
			case 4:
				write(sc,"nextone",7);
				break;
		}
	}
	return 0;
}
//审批员工的请假信息
int ApprovalLeave(LEAVE leave,int sc)
{
	int c;
	memset(&leave,0,sizeof(LEAVE));
	strcpy(leave.type,"approvalleave");
	LeavePack(&leave);//打包
	write(sc,p,strlen(p));
	while(1)
	{
		//清空p中之前的套接字数据
		memset(p,0,sizeof(p));
		int re = read(sc,p,1024);
		if (strncmp(p,"quit",4)==0)
		{
			break;
		}
		if (strncmp(p,"nothing",7)==0)
		{
			printf("查无信息\n");
			break;
		}
		printf("%s",p);
		printf("1--同意\n");
		printf("2--否决\n");
		printf("3--下一个\n");
		scanf("%d",&c);
		switch(c)
		{
			case 1:
				write(sc,"leaveyes",8);
				break;
			case 2:
				write(sc,"leaveno",7);
				break;
			case 3:
				write(sc,"nextone",7);
		}
		//给服务器发送接受成功信息isok
	}
}
//按照姓名查找员工的请假信息
int CheakLeaveName(LEAVE leave,int sc)
{
	int c;
	memset(&leave,0,sizeof(LEAVE));
	strcpy(leave.type,"cheakleavename");
	printf("input name:");
	scanf("%s",leave.name);
	LeavePack(&leave);//打包
	write(sc,p,strlen(p));
	while(1)
	{
		//清空p中之前的套接字数据
		memset(p,0,sizeof(p));
		int re = read(sc,p,1024);
		if (strncmp(p,"quit",4)==0)
		{
			break;
		}
		if (strncmp(p,"nothing",7)==0)
		{
			printf("查无信息\n");
			break;
		}
		printf("%s",p);
		//给服务器发送接受成功信息isok
		write(sc,"isok",4);
	}
}
//按时间查询每日打卡
int CheakLeaveTime(LEAVE leave,int sc)
{
	memset(&leave,0,sizeof(LEAVE));
	strcpy(leave.type,"checkleavetime");
	printf("input day:");
	scanf("%s",leave.leavetime);
	LeavePack(&leave);//打包
	write(sc,p,strlen(p));
	while(1)
	{
		//清空p中之前的套接字数据
		memset(p,0,sizeof(p));
		int re = read(sc,p,1024);
		if (strncmp(p,"quit",4)==0)
		{
			break;
		}
		if (strncmp(p,"nothing",7)==0)
		{
			printf("查无信息\n");
			break;
		}
		printf("%s",p);
		//给服务器发送接受成功信息isok
		write(sc,"isok",4);
	}
}